package job.manager.client.mb;

import com.icesoft.faces.component.ext.RowSelectorEvent;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import job.manager.db.ec.JobEB;
import job.manager.db.ec.ProfilesEB;
import job.manager.db.ec.TimeScheduleEB;
import job.manager.db.ec.TimeScheduleRowEB;
import job.manager.db.ec.UsersEB;
import job.manager.db.sb.JobRemote;
import job.manager.db.sb.ProfileRemote;
import job.manager.db.sb.TimeRemote;
import job.manager.db.sb.UserRemote;
import org.springframework.security.context.SecurityContextHolder;

/**
 *  The customer ManagedBean is responsible for connection to the Enterprise Beans
 *  and providing the pages/user with the information about customers jobs.
 *  Also responsible for editing, and removing jobs.
 *
 */
public class CustomerMB {

    @EJB
    private UserRemote userBean;
    @EJB
    private JobRemote jobBean;
    @EJB
    private TimeRemote timeBean;
    @EJB
    private ProfileRemote profileBean;
    private ArrayList<ProfilesEB> custList;
    private String customerid;
    private JobEB selectedJob;
    private String selectedJobName;
    private Date startDate;
    private Date stopDate;
    private String comment;
    private String createJobName;
    private String createComment;
    private Date createStartDate;
    private Date createEndDate;
    private int createNoOfPeople;
    ArrayList<JobEB> jobList;

    /** Creates a new instance of CustomerMB */
    public CustomerMB() {
        onInit();
    }

    @PostConstruct
    public void onInit() {
        this.problemSolver();
        this.checkIfNotAdmin();
    }

    //Actions

    public void createAction(ActionEvent arg) {
        TimeScheduleRowEB startRow = new TimeScheduleRowEB(null, createStartDate, "");
        TimeScheduleRowEB endRow = new TimeScheduleRowEB(null, createEndDate, "");
        startRow = timeBean.insert(startRow);
        endRow = timeBean.insert(endRow);
        TimeScheduleEB tSched = new TimeScheduleEB(null, 150);
        tSched.setStartid(startRow);
        tSched.setEndId(endRow);
        tSched = timeBean.insert(tSched);
        JobEB theNewJob = new JobEB(null, createNoOfPeople, createComment, createJobName);
        theNewJob.setTimeScheduleId(tSched);
        theNewJob.setCustomerId(profileBean.getProfileInfo(Integer.parseInt(customerid)));
        jobBean.insert(theNewJob);

    }

    public void updateAction(ActionEvent arg) {
        if (selectedJob != null) {
            jobBean.update(selectedJob);

        }
    }

    public void selectAction(RowSelectorEvent arg) {
        if (jobList != null) {
            this.selectedJob = (JobEB) jobList.get(arg.getRow());
        }
    }

    public void deleteAction(ActionEvent arg) {
        jobBean.delete(selectedJob);
        getJobsFromSelectedCustomer();
    }

    public void valueChanged(ValueChangeEvent e) {
        getJobsFromSelectedCustomer();
        selectedJob = null;
        startDate = null;
        stopDate = null;
        comment = null;
        updateValues();
    }

    //Our bug fix for spring secirity, if nullpointers get refrence from ConnectFactory
    private void problemSolver() {
        if (this.jobBean == null) {
            this.jobBean = ConnectFactory.getJobBean();
        }
        if (this.timeBean == null) {
            this.timeBean = ConnectFactory.getTimeBean();
        }
        if (this.profileBean == null) {
            this.profileBean = ConnectFactory.getProfileBean();
        }
        if (this.userBean == null) {
            this.userBean = ConnectFactory.getUserBean();
        }
    }

    //for user interface
    public boolean getIsEditing() {
        return (this.selectedJob == null);
    }

    protected void checkIfNotAdmin() {
        String userNamn = SecurityContextHolder.getContext().getAuthentication().getName();
        UsersEB currentUser = userBean.findUser(userNamn);
        if (currentUser.getUsergroup().getLevel() > 2) {
            this.customerid = currentUser.getProfileId().toString();
        }
    }

    //set and get methods

    public ArrayList<SelectItem> getCustomers() {
        this.problemSolver();
        custList = new ArrayList(profileBean.getCustomers());
        ArrayList<SelectItem> itemList = new ArrayList<SelectItem>();
        for (ProfilesEB pr : custList) {
            SelectItem a = new SelectItem();
            a.setLabel(pr.getName1() + " " + pr.getName2());
            a.setValue(pr.getId().toString());
            itemList.add(a);
        }
        return itemList;
    }

    public String getSelectedCustomerName() {
        if (customerid != null) {
            ProfilesEB p = profileBean.getProfileInfo(Integer.parseInt(customerid));
            selectedJobName = p.getName1() + " " + p.getName2();

        } else {
            selectedJobName = "no customer selected";
        }

        return selectedJobName;
    }

    public Collection<JobEB> getJobsFromSelectedCustomer() {
        this.problemSolver();
        this.checkIfNotAdmin();
        if (customerid == null) {
            customerid = "1";
        }
        jobList = new ArrayList<JobEB>(profileBean.getJobsByPid(Integer.parseInt(customerid)));
        updateValues();
        return jobList;

    }

    public String getSelectedJobName() {
        if (selectedJob != null) {
            return selectedJob.getName();
        }
        return "no job selected";
    }

    public void setSelectedJobName(String jobname) {
        if (selectedJob != null) {
            selectedJob.setName(jobname);
        }
    }

    public int getNoOfPeople() {
        if (selectedJob != null) {
            return selectedJob.getNumberOfPeople();
        }
        return 0;
    }

    public void setNoOfPeople(int noOfPeople) {
        if (selectedJob != null) {
            selectedJob.setNumberOfPeople(noOfPeople);
        }
    }

    private void updateValues() {
        getSelectedJobName();
    }

    public Date getStartDate() {
        if (selectedJob != null) {
            startDate = selectedJob.getTimeScheduleId().getStartid().getDateTime();
        }
        return startDate;
    }

    public void setStartDate(Date startDate) {
        if (selectedJob != null) {
            selectedJob.getTimeScheduleId().getStartid().setDateTime(startDate);
        }
    }

    public Date getStopDate() {
        if (selectedJob != null) {
            stopDate = selectedJob.getTimeScheduleId().getEndId().getDateTime();
        }
        return stopDate;
    }

    public void setStopDate(Date stopDate) {
        if (selectedJob != null) {
            selectedJob.getTimeScheduleId().getEndId().setDateTime(stopDate);
        }
    }

    public String getCustomerid() {
        this.problemSolver();
        this.checkIfNotAdmin();
        return customerid;
    }

    public void setCustomerid(String customerid) {
        this.customerid = customerid;
    }

    public String getComment() {
        if (selectedJob != null) {
            comment = selectedJob.getComment();
        }
        return comment;
    }

    public void setComment(String comment) {
        if (selectedJob != null) {
            selectedJob.setComment(comment);
            timeBean.update(selectedJob.getTimeScheduleId());
            timeBean.update(selectedJob.getTimeScheduleId().getStartid());
            timeBean.update(selectedJob.getTimeScheduleId().getEndId());
        }
    }

    public String getCreateComment() {
        return createComment;
    }

    public void setCreateComment(String createComment) {
        this.createComment = createComment;
    }

    public Date getCreateEndDate() {
        return createEndDate;
    }

    public void setCreateEndDate(Date createEndDate) {
        this.createEndDate = createEndDate;
    }

    public String getCreateJobName() {
        return createJobName;
    }

    public void setCreateJobName(String createJobName) {
        this.createJobName = createJobName;
    }

    public int getCreateNoOfPeople() {
        return createNoOfPeople;
    }

    public void setCreateNoOfPeople(int createNoOfPeople) {
        this.createNoOfPeople = createNoOfPeople;
    }

    public Date getCreateStartDate() {
        return createStartDate;
    }

    public void setCreateStartDate(Date createStartDate) {
        this.createStartDate = createStartDate;
    }
}